梦入琼楼寒有月,行过石树冻无烟

D3 雷达图

HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>D3.js</title>
<link rel="stylesheet" type="text/less" href="less/style.less">
<script src="js/less.js"></script>
<script src="https://d3js.org/d3.v4.0.0-alpha.50.min.js"></script>
</head>
<body>
<script src="js/style.js"></script>
<div id="chart"></div>
</body>
<script>
</script>
</html>

JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var padding = {
width: 400,
height: 400
}

var svg = d3.select("body")
.selectAll("svg")
.append("svg")
.attr("width", padding.width)
.attr("height",padding.height)
/*
* config
* w: width 300
* h: height 300
* maxValue 最大值为 100
* levels 分别在五个外圈中标注文字
* ExtrExtraWidthX 额外宽度为 300
*/
var config = {
w: padding.width,
h: padding.height,
maxValue: 100,
levels: 5,
ExtraWidthX: 300
}

d3.json("./data.json", function(error, data) {
if (error) throw error;
RadarChart.draw("#chart", data, config);
});

var RadarChart = {
draw: function (id,d,options) {
var cfg = {
radius: 5,
w: 600,
h: 600,
factor: 1,
factorLegend: .85,
levels: 3,
maxValue: 0,
radians: 2 * Math.PI,
opacityArea: 0.5,
ToRight: 5,
TranslateX: 80,
TranslateY: 30,
ExtraWidthX: 100,
ExtraWidthY: 100,
color: d3.scaleOrdinal()
.range(["#037ef3"], "#bbd4ff")
}

if ('undefined' !== typeof options) {
for (var i in options) {
if ('undefined' !== typeof options[i]) {
cfg[i] = options[i]
}
}
}
cfg.maxValue = 100

var allAxis = (d[0].map(function (i, j) {
return i.area
}))
var total = allAxis.length
var radius = cfg.factor * Math.min(cfg.w/2, cfg.h/2)
var Format = d3.format('%')

d3.select(id).select("svg")
.remove()

var g = d3.select(id)
.append("svg")
.attr("width",cfg.w+cfg.ExtraWidthX)
.attr("height",cfg.h+cfg.ExtraWidthY)
.append("g")
.attr("transform", "translate(" + cfg.TranslateX + "," + cfg.TranslateY + ")")

var tooltip

/*
绘制圆形线段
*/
for(var j=0;j<cfg.levels;j++) {
var levelFactor = cfg.factor * radius * ((j + 1) / cfg.levels)
g.selectAll(".levels")
.data(allAxis)
.enter("")
.append("attr:line")
.attr("x1", function (d, i) {
return levelFactor * (1 - cfg.factor * Math.sin(i * cfg.radians / total))
})
.attr("y1", function (d, i) {
return levelFactor * (1 - cfg.factor * Math.cos(i * cfg.radians / total))
})
.attr("x2", function (d, i) {
return levelFactor*(1 - cfg.factor * Math.sin((i + 1) * cfg.radians / total))
})
.attr("y2", function (d, i) {
return levelFactor* (1 - cfg.factor * Math.cos((i + 1) * cfg.radians / total))
})
.attr("class", "line")
.style("stroke", "#037ef3")
.style("stroke-opacity", "0.75")
.style("stroke-width", "0.3px")
.attr("transform", "translate(" + (cfg.w / 2 - levelFactor) + "," + (cfg.h / 2 - levelFactor) + ")")
}
/*
* 绘制每个外线的百分比文本
*/
for (var j=0;j<cfg.levels;j++) {
var leveFactor = cfg.factor*radius*((j+1)/cfg.levels)
g.selectAll(".levels")
.data([1])
.enter()
.append("svg:text")
.attr("x", function (d) {
return leveFactor*(1-cfg.factor*Math.sin(0))
})
.attr("y", function (d) {
return leveFactor*(1-cfg.factor*Math.cos(0))
})
.attr("class","legend")
.style("font-weight","100")
.style("font-size","10px")
.attr("transform", "translate(" + (cfg.w/2-leveFactor + cfg.ToRight) + "," + (cfg.h/2-leveFactor) + ")")
.attr("fill", "#202020")
.text((j+1)*100/cfg.levels)
}
/*
* 绘制坐标系
*/
series = 0
var axis = g.selectAll(".axis")
.data(allAxis)
.enter()
.append("g")
.attr("class","axis")

axis.append("line")
.attr("x1", cfg.w/2)
.attr("y1", cfg.h/2)
.attr("x2", function (d,i) {
return cfg.w/2*(1-cfg.factor*Math.sin(i*cfg.radians/total))
})
.attr("y2", function (d,i) {
return cfg.h/2*(1-cfg.factor*Math.cos(i*cfg.radians/total))
})
.attr("class","line")
.style("stroke","#738796")
.style("stroke-width","1px")

/*
* 绘制文字
*/
axis.append("text")
.attr("class","legend")
.text(function (d) {
return d
})
.style("font-family","sans-serif")
.style("fill","#202020")
.style("font-size","11px")
.attr("text-anchor","middle")
.attr("dy","1.5em")
.attr("transform", function (d,i) {
return "translate(0,-10)"
})
.attr("x", function (d,i) {
return cfg.w/2*(1-cfg.factorLegend*Math.sin(i*cfg.radians/total))-60*Math.sin((i*cfg.radians/total))
})
.attr("y", function (d,i) {
return cfg.h/2*(1-Math.cos(i*cfg.radians/total))-20*Math.cos(i*cfg.radians/total)
})
/*
* 绘制数据
* 与添加鼠标事件
*/
d.forEach(function (y,x) {
dataValues = []
g.selectAll(".nodes")
.data(y, function (j,i) {
dataValues.push([
cfg.w/2*(1-(parseFloat(Math.max(j.value, 0))/cfg.maxValue)*cfg.factor*Math.sin(i*cfg.radians/total)),
cfg.h/2*(1-(parseFloat(Math.max(j.value, 0))/cfg.maxValue)*cfg.factor*Math.cos(i*cfg.radians/total))
])
})
dataValues.push(dataValues[0])
g.selectAll(".area")
.data([dataValues])
.enter()
.append("polygon")
.attr("class","radar-chart-serie"+series)
.style("stroke",cfg.color(series))
.style("stroke-width","2px")
.attr("points", function (d) {
var str="";
for (var pti=0;pti<d.length;pti++) {
str=str+d[pti][0]+ "," + d[pti][1]+ " "
}
return str
})
.style("fill", function (j,i) {
return cfg.color(series)
})
.style("fill-opacity",cfg.opacityArea)
.on("mouseover", function (d) {
z = "polygon." + d3.select(this).attr("class")
g.selectAll("polygon")
.transition(200)
.style("fill-opacity", 0.1)
g.selectAll(z)
.transition(200)
.style("fill-opacity",.7)
})
.on("mouseout", function () {
g.selectAll("polygon")
.transition(200)
.style("fill-opacity", cfg.opacityArea)
})
series++
})
series=0
/*
* 绘制边界点
*/
var tooltip = d3.select("body")
.append("div")
.attr("class","toolTip")
d.forEach(function(y, x){
g.selectAll(".nodes")
.data(y)
.enter()
.append("svg:circle")
.attr("class","radar-chart-serie"+series)
.attr("r",cfg.radius)
.attr("alt",function (j) {
return Math.max(j.value,0)
})
.attr("cx", function(j, i) {
dataValues.push([
cfg.w / 2 * (1 - (parseFloat(Math.max(j.value, 0)) / cfg.maxValue) * cfg.factor * Math.sin(i * cfg.radians / total)),
cfg.h / 2 * (1 - (parseFloat(Math.max(j.value, 0)) / cfg.maxValue) * cfg.factor * Math.cos(i * cfg.radians / total))
])
return cfg.w / 2 * (1 - (Math.max(j.value, 0) / cfg.maxValue) * cfg.factor * Math.sin(i * cfg.radians / total))
})
.attr("cy", function (j,i) {
return cfg.h/2*(1-(Math.max(j.value,0)/cfg.maxValue)*cfg.factor*Math.cos(i*cfg.radians/total))
})
.attr("data-id", function (j) {
return j.area
})
.style("fill","#fff")
.style("stroke-width","2px")
.style("stroke",cfg.color(series))
.style("fill-opacity",.9)
.on("mouseover", function (d) {
tooltip
.style("left",d3.event.pageX - 40 + "px")
.style("top", d3.event.pageY -80 + "px")
.style("display","inline-block")
.html((d.area) + "<br><span>" + (d.value) + "</span>")
})
.on("mouseout", function (d) {
tooltip.style("display","none")
series++
})
})
}
}

JSON

1
2
3
4
5
6
7
8
9
10
[
[
{"area": "端口协议漏洞 ", "value": 80},
{"area": "客户端涉及端口漏洞", "value": 50},
{"area": "端口威胁情报 ", "value": 90},
{"area": "协议逻辑漏洞", "value": 90},
{"area": "端口服务漏洞", "value": 60},
{"area": "协议底层漏洞", "value": 20}
]
]

CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#chart {
margin-top: 10%;
margin-left: 10%;
}

.toolTip {
pointer-events: none;
position: absolute;
display: none;
min-width: 50px;
height: auto;
background: #ffffff;
padding: 5px 14px 6px 14px;
border-radius: 3px;
text-align: center;
line-height: 1.3;
color: #5B6770;
font-size: 10px;
box-shadow: 1px 1px 1px rgb(208, 208, 208);
}
.toolTip:after {
content: "";
width: 0;
height: 0;
border-left: 12px solid transparent;
border-right: 0px solid transparent;
border-top: 12px solid white;
position: absolute;
bottom: -10px;
left: 50%;
margin-left: -12px;
}
.toolTip span {
font-weight: bold;
color: #037ef3;
}
⬅️ Go back